#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

#define MIN(x, y) ((x) < (y) ? (x) : (y))

int show(int *a, int len)
{
    for (int i = 0; i < len; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

int minimumTotal(int **triangle, int triangleSize, int *triangleColSize)
{
    for (int i = triangleSize - 2; i >= 0; i--) {
        for (int k = 0; k < triangleColSize[i]; k++) {
            triangle[i][k] += MIN(triangle[i + 1][k], triangle[i + 1][k + 1]);
        }
    }
    return triangle[0][0];
}

int main(int argc, char const *argv[])
{
    int triangleSize = 4;
    int **triangle = (int **) malloc(triangleSize * sizeof(int *));
    int data[4][4] = {
            { 2 },
            { 3, 4 },
            { 6, 5, 7 },
            { 4, 1, 8, 3 },
    };
    triangle[0] = data[0];
    triangle[1] = data[1];
    triangle[2] = data[2];
    triangle[3] = data[3];

    int triangleColSize[] = {1,2,3,4};
    int minPath = minimumTotal(triangle, triangleSize, triangleColSize);
    printf("%d\n", minPath);

    return 0;
}